package visiopuce.ui;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class AnneeDialog extends Dialog {
	
	
	private Combo comboAnnee;
	private Button btnOk;
	private Button btnAnnuler;
	String titre;
	private String question;
	private Integer annee;


	public AnneeDialog(Shell parentShell, String titre, String question) {
		super(parentShell);
		this.titre = titre;
		this.question = question;
		setShellStyle(SWT.CLOSE | SWT.TITLE | SWT.BORDER
				| SWT.APPLICATION_MODAL);
		setBlockOnOpen(true);
	}

	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText(titre);
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout layout = new GridLayout(2, false);
		layout.marginRight = 5;
		layout.marginLeft = 10;
		container.setLayout(layout);

		Label lblQuestion = new Label(container, SWT.NONE);
		lblQuestion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblQuestion.setText(question);
		
		
		comboAnnee = new Combo(container, SWT.SINGLE | SWT.READ_ONLY);
		GridData gridData = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gridData.widthHint = 150;
		comboAnnee.setLayoutData(gridData);
		comboAnnee.setItems(new String[] { "2015", "2014", "2013", "2012", "2011" });


		return container;
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, "Ok", true);
		createButton(parent, IDialogConstants.CANCEL_ID, "Annuler", false);
	}

	@Override
	protected Point getInitialSize() {
		return new Point(255, 198);
	}

	@Override
	protected void okPressed() {
		try { 
			annee = Integer.parseInt(comboAnnee.getText());
	    } catch(NumberFormatException e) { 
	    	annee = null; 
	    }
		
		super.okPressed();
	}

	public Integer getAnnee() {
		return annee;
	}
	
	

}